package com.codeforces.contest.c305;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class _305C {

	static Scanner scanner = new Scanner(System.in);
	static int n, a, largest;
	static Set<Integer> m = new HashSet<Integer>();

	public static void main(String[] args) {
		n = scanner.nextInt();
		largest = -1;
		for (int i=0;i<n;++i) {
			a = scanner.nextInt();
			if (!m.contains(a)) {
				m.add(a);
				if (a >largest) {
					largest = a;
				}
			}
			else {
				do {
					m.remove(a);
					if (!m.contains(a+1)) {
						m.add(a+1);
						if (a + 1 > largest) {
							largest = a + 1;
						}
						break;
					}
					else {
						a = a+1;
					}
				} while(true);
			}
		}
		int ss = m.size();
		System.out.println(largest + 1 - ss);
	}

}
